package com.github.aloxc.plugin.restplus.navigator;


import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.listener.*;
import com.github.aloxc.plugin.restplus.listener.search.SearchListener;
import com.github.aloxc.plugin.restplus.navigation.action.RestServiceItem;
import com.intellij.openapi.actionSystem.DataKey;
import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.components.JBPanel;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.MouseListener;
import java.util.List;

import static com.github.aloxc.plugin.restplus.common.Consts.TOOLBAR_BUTTON_DIMENSION;

/**
 *
 * @author leero
 */
public class RestServicesNavigatorPanel extends SimpleToolWindowPanel implements DataProvider {
    public static final DataKey<List<RestServiceItem>> SERVICE_ITEMS = DataKey.create("SERVICE_ITEMS");
    private final Project myProject;

    public JTree getTree() {
        return myTree;
    }

    private final JTree myTree;
    RestServiceDetail myRestServiceDetail;
    private JSplitPane servicesContentPaneJsplitPane;
    private Splitter servicesContentPaneSplitter ;

    public Project getProject() {
        return myProject;
    }
    public RestServicesNavigatorPanel(Project project, JTree tree) {
        super(true, true);
        myProject = project;
        myTree = tree;
        myRestServiceDetail = project.getComponent(RestServiceDetail.class);
        myRestServiceDetail.setUrlTree(myTree);
//        Log.log("RestServicesNavigatorPanel","开始从Panel中设置 Detail的 project " + project);
        myRestServiceDetail.setProject(myProject);
        Color gray = new Color(255,162,45);
        FlowLayout layout = new FlowLayout();
        layout.setAlignment(FlowLayout.LEADING);
        JBPanel tb = new JBPanel<>();
        tb.setLayout(layout);
        layout.setAlignment(FlowLayout.LEADING);
        tb.setLayout(layout);

        tb.add(createButton("refresh",new RefreshListener(project)));
        tb.add(createButton("search",new SearchListener(project)));
//        tb.add(createButton("run",new RunAllListener(project)));
//        tb.add(createButton("postman",new PostmanListener(project)));
        tb.add(createButton("swagger",new SwaggerListener(project)));
        tb.add(createButton("setting",new SettingListener(project)));
        tb.add(createButton("environment",new ChangeEnvironmentListener(project)));
        tb.add(createButton("add",new AddUrlListener(project)));
        tb.add(createButton("group",new GroupListener(project)));
        tb.add(createButton("sort",new SortListener(project)));
        tb.add(createButton("show",new ShowListener(project)));
        tb.add(createButton("language",new LanguageListener(project)));
        tb.add(createButton("history",new HistoryMouseListener(myRestServiceDetail,true)));
        tb.add(createButton("output",new OutputListener(myRestServiceDetail)));
        tb.add(createButton("tools",new ToolsListener(myRestServiceDetail)));
        tb.add(createButton("help",new HelpListener(myProject)));
//        tb.add(createButton("donate",new DonateFormListener(myProject)));
        setToolbar(tb);

        JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myTree);
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.RED));
        scrollPane.setBackground(gray);

        servicesContentPaneSplitter = new Splitter(true,  .5f);
        servicesContentPaneSplitter.setShowDividerControls(true);
        servicesContentPaneSplitter.setDividerWidth(10);
        servicesContentPaneSplitter.setBackground(gray);
        servicesContentPaneSplitter.setBorder(BorderFactory.createLineBorder(gray));
        scrollPane.setBackground(gray);
        servicesContentPaneSplitter.setFirstComponent(scrollPane);
        servicesContentPaneSplitter.setSecondComponent(myRestServiceDetail);
        setContent(servicesContentPaneSplitter);
    }

    private JButton createButton(String name, MouseListener listener){
        JButton btn = new JButton();
        btn.setPreferredSize(TOOLBAR_BUTTON_DIMENSION);
        btn.setIcon(IconLoader.getIcon("/icons/"+name+".png"));
        btn.setUI(new BasicButtonUI());
        btn.setRolloverIcon(IconLoader.getIcon("/icons/"+name+"18.png"));
        btn.setBorder(BorderFactory.createRaisedBevelBorder());
        btn.setBorderPainted(false);
        btn.setOpaque(false);
        btn.setRolloverEnabled(true);
        btn.setFocusPainted(false);
        btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btn.setToolTipText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.toolbar." + name + ".text"));
        btn.addMouseListener(listener);
        return btn;
    }


    @Override
    @Nullable
    public Object getData(@NonNls String dataId) {
        return super.getData(dataId);
    }

}


